package com.example.demo;

import org.jetbrains.annotations.NotNull;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.util.regex.Pattern.*;

/**
 * @Description
 * @Author
 * @Date 2019/9/26 21:21
 **/
public class StringMatchDemo {
    public static void main(String[] args) throws IOException {

        String regex = inputStream2String(ClassLoader.getSystemResourceAsStream("desc.text"));
        Html2Text(regex);
        regex=regex.replaceAll("\r|\n|\t"," ");
//        regex=regex.replaceAll("<.*?>"," ");
        regex = regex.replaceAll("<style>[^>]*</style>", "");
        String txtcontent = regex.replaceAll("</?[^>]+>", ""); //剔出<html>的标签
        txtcontent = txtcontent.replaceAll("<a>\\s*|\t|\r|\n</a>", "");
        regex =regex.replaceAll("<.*?>|&nbsp;|\\[|\\]|;|,|\\?|\n|\t", " ");
//        List<String>  strings= new ArrayList<>();
//        for (int i = 0; i < 1000000; i++) {
//            strings.add("asdasdadsad精灵娃娃和鹿图形商标dasdasd");
//        }
//
//        regex=".*精灵娃娃和鹿图形商标.*";
//        long start = System.currentTimeMillis();
//        for (String string : strings) {
//            string.matches(regex);
//        }
//        System.out.println((System.currentTimeMillis()-start));

//        Pattern compile = compile("(?>[^\"]*)");
        Pattern compile = compile("<body>(.+?)</body>");
        Matcher matcher = compile.matcher(regex);
        while (matcher.find()){
            String group = matcher.group(1);
            System.out.println(group);
        }
//        new Thread(()->{
//            System.out.println("ok");
//        });
    }
    //从html中提取纯文本
    public static String Html2Text(String inputString) {
        String htmlStr = inputString; // 含html标签的字符串
        String textStr = "";
        java.util.regex.Pattern p_script;
        java.util.regex.Matcher m_script;
        java.util.regex.Pattern p_style;
        java.util.regex.Matcher m_style;
        java.util.regex.Pattern p_html;
        java.util.regex.Matcher m_html;
        try {
            String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>"; // 定义script的正则表达式{或<script[^>]*?>[\\s\\S]*?<\\/script>
            String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"; // 定义style的正则表达式{或<style[^>]*?>[\\s\\S]*?<\\/style>
            String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
            p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
            m_script = p_script.matcher(htmlStr);
            htmlStr = m_script.replaceAll(""); // 过滤script标签
            p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
            m_style = p_style.matcher(htmlStr);
            htmlStr = m_style.replaceAll(""); // 过滤style标签
            p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
            m_html = p_html.matcher(htmlStr);
            htmlStr = m_html.replaceAll(""); // 过滤html标签
            textStr = htmlStr;
        } catch (Exception e) {
            System.err.println("Html2Text: " + e.getMessage());
        }
        //剔除空格行
        textStr = textStr.replaceAll("[ ]+", " ");
        textStr = textStr.replaceAll("(?m)^\\s*$(\\n|\\r\\n)", "");
        return textStr;// 返回文本字符串
    }

    @NotNull
    public static String inputStream2String(InputStream in) throws IOException {
      BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(new BufferedInputStream(in)));
      StringBuffer out = new StringBuffer();
      String s;
      while ((s = bufferedReader.readLine())!=null){
          out.append(s);
          out.append(NewLine());
      }
        return out.toString();
    }


    /**
     * 获取系统换行符
     *
     * @return
     */
    public static String NewLine() {
        return System.getProperty("line.separator", "\r\n");
    }
}
